<template>
  <view class="box" v-show="goods_info.goods_name">
    <!-- 轮播图 -->
    <swiper :indicator-dots="true" :autoplay="true" :interval="5000" :duration="1000" :circular="true">
      <swiper-item v-for="(pic, index) in goods_info.pics" :key="index">
        <image :src="pic.pics_big" mode="widthFix" @click="previewImage(index)"></image>
      </swiper-item>
    </swiper>
    <!-- 商品信息 -->
    <view class="goods_info">
      <view class="goods_price">￥{{goods_info.goods_price}}</view>
      <view class="goods_title">
        <view class="goods_desc">{{goods_info.goods_name}}</view>
        <view class="collect">
          <uni-icons type="star" size="18" color="gray"></uni-icons>
          <text>收藏</text>
        </view>
      </view>
      <view class="yf">快递：免运费</view>
    </view>
    <!-- 详情 -->
    <rich-text :nodes="goods_info.goods_introduce"></rich-text>
    <!-- 底部购物车相关 -->
    <view class="goods_nav">
      <uni-goods-nav :fill="true" :options="options" :buttonGroup="buttonGroup" @click="infoAbout" @buttonClick="buyAbout"/>
    </view>
  </view>
</template>

<script>
  import {mapMutations, mapGetters} from 'vuex'
  export default {
    data() {
      return {
        // 商品详情对象
        goods_info: {},
        // 商品底部导航
        options: [{
          icon: 'shop',
          text: '店铺'
        }, {
          icon: 'headphones',
          text: '客服',
          infoColor: "red"
        }, {
          icon: 'cart',
        	text: '购物车',
          info: 0
        }],
        buttonGroup: [{
            text: '加入购物车',
            backgroundColor: '#ffa200',
            color: '#fff'
          },
          {
            text: '立即购买',
            backgroundColor: '#ff0000',
            color: '#fff'
          }
        ]
      };
    },
    onLoad(options) {
      this.getGoodsDetail(options);
    },
    computed: {
      ...mapGetters('cartAbout', ['total'])
    },
    watch: {
      // 购物车小图标数量
      total: {
        handler(newVal) {
          this.options[2].info = newVal
        },
        // 页面初次加载完毕后立即调用
        immediate: true
      }
    },
    methods: {
      // store里面的方法
      ...mapMutations('cartAbout', ['ADDTOCART']),
      // 获取商品详情数据
      async getGoodsDetail(options) {
        const result = await uni.$http.get('/api/public/v1/goods/detail', {
          goods_id: options.goods_id
        });
        if (result.data.meta.status !== 200) return uni.$showMsg();
        // 解决图片之间的间隙问题    解决在IOS上图片无法显示的问题
        result.data.message.goods_introduce = result.data.message.goods_introduce.replace(/<img /g,
          '<img style="display:block;" ').replace(/webp/g, 'jpg')
        this.goods_info = result.data.message;
      },
      // 预览图片
      previewImage(index) {
        uni.previewImage({
          // 当前图片
          current: index,
          // 所有要预览的图片的url
          urls: this.goods_info.pics.map(item => item.pics_big)
        })
      },
      // 底部导航左侧按钮：店铺、客服、购物车
      infoAbout(e) {
        if (e.content.text === '购物车') {
          uni.switchTab({
            url: "/pages/cart/cart"
          })
        } else return uni.$showMsg('暂未开发')
      },
      // 底部导航右侧按钮：加入购物车、立即购买
      buyAbout(e) {
        const { goods_id, goods_name, goods_price, goods_small_logo } = this.goods_info
        if (e.content.text === '加入购物车') {
          // 商品id|名称|价格|图片|数量|状态
          const good = {
            goods_id,
            goods_name,
            goods_price,
            goods_small_logo,
            goods_count: 1,
            goods_state: true
          }
          this.ADDTOCART(good);
        }
      }
    }
  }
</script>

<style lang="scss">
  .box {
    background-color: white;
    padding-bottom: 50px;
  }
  // 轮播图
  swiper {
    height: 750rpx;
    image {
      width: 100%;
      height: 100%;
    }
  }
  // 商品详情
  .goods_info {
    padding: 10px;
    padding-right: 0;
    .goods_price {
      color: #C00000;
      font-size: 18px;
      margin: 10px 0;
    }
    .goods_title {
      display: flex;
      justify-content: space-between;
      .goods_desc {
        font-size: 13px;
        padding-right: 10px;
      }
      .collect {
        width: 120px;
        font-size: 12px;
        display: flex;
        flex-direction: column;
        justify-content: center;
        align-items: center;
        border-left: 1px solid #efefef;
        color: gray;
      }
    }
    .yf {
      font-size: 12px;
      margin: 10px 0;
      color: gray;
    }
  }
  // 商品导航
  .goods_nav {
    position: fixed;
    bottom: 0;
    left: 0;
    width: 100%;
    z-index: 999;
    height: 50px;
  }
</style>
